package com.cdtye.shuoh.service.device.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtye.shuoh.domain.device.entity.DeviceChannel;
import com.cdtye.shuoh.domain.device.vo.DeviceChannelVo;
import com.cdtye.shuoh.mapper.device.DeviceChannelMapper;
import com.cdtye.shuoh.service.device.DeviceChannelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeviceChannelServiceImpl extends ServiceImpl<DeviceChannelMapper, DeviceChannel> implements DeviceChannelService {

    @Autowired
    private DeviceChannelMapper deviceChannelMapper;

    public IPage<DeviceChannel> pageList(DeviceChannelVo vo) {
        return deviceChannelMapper.pageList(vo.page(),vo);
    }

    public List<DeviceChannel> findListByDeviceIdentification(String deviceId){
        QueryWrapper<DeviceChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("DEVICE_IDENTIFICATION",deviceId);
        return deviceChannelMapper.selectList(wrapper);
    }

    public DeviceChannel findByChannelId(String channelId){
        QueryWrapper<DeviceChannel> wrapper = new QueryWrapper<>();
        wrapper.eq("CHANNEL_ID",channelId);
        return deviceChannelMapper.selectOne(wrapper);
    }

    public boolean saveOrUpdateDeviceChannel(DeviceChannel deviceChannel){
        return this.saveOrUpdate(deviceChannel);
    }


    public DeviceChannel queryChannel(String deviceId, String channelId) {
        return deviceChannelMapper.queryChannel(deviceId,channelId);
    }

    @Override
    public boolean updateChannel(DeviceChannel deviceChannel) {
        UpdateWrapper<DeviceChannel> wrapper = new UpdateWrapper<>();
        wrapper.eq("DEVICE_IDENTIFICATION",deviceChannel.getDeviceIdentification());
        wrapper.eq("CHANNEL_ID",deviceChannel.getChannelId());
        return this.update(deviceChannel,wrapper);
    }

    @Override
    public void stopPlay(String deviceIdentification, String channelId) {
        deviceChannelMapper.stopPlay(deviceIdentification,channelId);
    }

    @Override
    public void startPlay(String deviceIdentification, String channelId, String streamId) {
        deviceChannelMapper.startPlay(deviceIdentification,channelId,streamId);
    }

    @Override
    public void updateChannel(String deviceIdentification, DeviceChannel channel) {
        String channelId = channel.getChannelId();
        channel.setDeviceIdentification(deviceIdentification);
        DeviceChannel deviceChannel = deviceChannelMapper.queryChannel(deviceIdentification, channelId);
        if (deviceChannel == null) {
            this.save(channel);
        }else {
            this.updateChannel(channel);
        }
    }

    /**
     * 清空通道
     * @param deviceIdentification
     */
    @Override
    public void cleanChannelsForDevice(String deviceIdentification) {
        deviceChannelMapper.cleanChannelsByDeviceId(deviceIdentification);
    }
}
